Ext.define('truckplanner.controller.Contacts', {
    extend: 'Ext.app.Controller',

    stores : ['Contacts'],
    models : ['Contact'],
    views: ['contact.List','contact.Edit'],
    
    requires: ['truckplanner.view.contact.Edit', 'Ext.MessageBox'],
    
    refs: [
           {ref: 'contactList', selector: 'contactlist'},
           {ref: 'contactData', selector: 'contactlist dataview'},
           {
               ref: 'contactEditor', 
               selector: 'contacteditor', 
               autoCreate: true,
               xtype: 'contacteditor'
           },
           {ref: 'contactForm', selector: 'contacteditor form'}
    ],
    
    init: function() {
    	this.control({
            'contactlist button[action=remove]': {
                click: this.removeContact
            },
            'contactlist button[action=edit]': {
                click: this.editContact
            },
            'contactlist button[action=add]': {
                click: this.addContact
            }
        });
    }, 
    
    /**
	 * React to the edit button being clicked.
	 * 
	 * @private
	 */
    
    editContact: function() {
    	var list = this.getContactList(),
    	editor = this.getContactEditor(),
    	form = this.getContactForm(),
    	contact = list.getSelectionModel().getSelection()[0];
    	this.editedContact = contact;
    	this.control({
    			'contacteditor button[action=create]': {
                click: this.saveContact
            }
    	});
    	fields = form.getForm().getFields();
        fields.each(function(field){
        	if(field.xtype != 'container' && field.xtype != 'button')
        		field.setValue(contact.get(field.getName()));
        });
    	editor.show();
    },
    
    saveContact: function() {
    	var win = this.getContactEditor(),
    	list = this.getContactList(),
		form = this.getContactForm(),
		contact = list.getSelectionModel().getSelection()[0],
		store = this.getContactsStore();
		fields = form.getForm().getFields();
        fields.each(function(field){
        	if(field.xtype != 'container' && field.xtype != 'button')
        		contact.set(field.getName(), field.getValue());
        });
		store.save();
        form.getForm().reset();
		win.hide();
    },
    
    /**
	 * React to the add button being clicked.
	 * 
	 * @private
	 */
    
    addContact: function() {
    	this.control({
    		'contacteditor button[action=create]': {
                click: this.createContact
            }
    	});
    	this.getContactEditor().show();    	
    },
    
	createContact: function() {
    	var win = this.getContactEditor(),
		form = this.getContactForm(),
		newContact = this.getContactModel().create({}),
		store = this.getContactsStore();
		fields = form.getForm().getFields();
        fields.each(function(field){
        	if(field.xtype != 'container' && field.xtype != 'button')
    			newContact.set(field.getName(), field.getValue());
        });
		store.add(newContact);
		store.save();
        form.getForm().reset();
		win.hide();
	},
    
    /**
	 * React to the remove button being clicked.
	 * 
	 * @private
	 */
    removeContact: function() {
    	var list = this.getContactList();
    	contact = list.getSelectionModel().getSelection();
        if(contact.length > 0)
        {
        	Ext.MessageBox.confirm('Message', 'Etes-vous sure de vouloir le supprimer?' , this.removeContact2, this);	
        }
	},
	
	removeContact2 : function () {
		var store = this.getContactsStore();
     	store.remove(this.getContactList().getSelectionModel().getSelection()[0]);
     	store.save();
	}
});